package Recursion;
public class MiGong {
    public static void main(String[] args) {
        //创建棋盘
        int map[][] = new int[8][7];
        for (int i = 0; i < 8; i++) {
            map[i][0] = 1;
            map[i][6] = 1;
        }
        for (int i = 0; i < 7; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }
        map[3][1]=1;
        map[3][2]=1;
        map[2][2]=1;

        //打印初始迷宫
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("=================================");


        Step step = new Step();//实例化
        step.gostep(map,1,1);//开始走



        //打印结果
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
    }

}
//0代表可以走但还未走
//1代表墙面
//2代表走的路径
//3代表试探过但是不可行的路程
//判断优先级是下右上左
class Step{
    public boolean gostep(int map[][],int a,int b){
        if (map[6][5] == 2){
            return true;
        }
        if(map[a][b]==0){
            map[a][b]=2;
            if(gostep(map,a+1,b)){
                return true;
            }
            else if(gostep(map,a,b+1)){
                return true;
            }
            else if(gostep(map,a-1,b)){
                return true;
            }
            else if(gostep(map,a,b-1)){
                return true;
            }
            else {
                map[a][b]=3;
                return false;
            }
        }
        else return false;
    }
}